﻿using System;
using System.Linq;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using System.Collections.Generic;

namespace Wicresoft.EKA.ITSM.BusinessImpl
{
    public class SMGXJPlanTicket : Base, ISMGXJPlanTicket
    {
        protected override void Dispose(bool disposing)
        {
        }

        public string GetTicketID()
        {
            WSGetEntryModel model = new WSGetEntryModel();
            model.ShortDescription = DateTime.Now.ToString();
            var proxy = this.GetARProxy<WSGetEntryModel>();
            proxy.SetEntryByQuery(model, "'Type'=\"XunJian\"", BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction, BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyFirst);           
            var list = proxy.GetListEntry("'Type'=\"XunJian\"", new System.Collections.Generic.List<uint>()
                {
                    WSGetEntryModel.CharacterField_ID,
                }).FirstOrDefault().CharacterField.ToString();
            return list;
        }

        public SMGXJPlanTicketModel GetbyID(string RequestID)
        {
            var proxy = this.GetARProxy<SMGXJPlanTicketModel>();
            var list = proxy.GetListEntry("'cChr_TicketID'=\""+RequestID+"\"", new System.Collections.Generic.List<uint>()
                {
                    SMGXJPlanTicketModel.cChr_Department_ID,
                    SMGXJPlanTicketModel.cChr_Description_ID,
                    SMGXJPlanTicketModel.cChr_FullName_ID,
                    SMGXJPlanTicketModel.cChr_MarkAsDelete_ID,
                    SMGXJPlanTicketModel.cChr_Name_ID,
                    SMGXJPlanTicketModel.cChr_Office_ID,
                    SMGXJPlanTicketModel.CreateDate_ID,
                    SMGXJPlanTicketModel.LastModifiedBy_ID,
                    SMGXJPlanTicketModel.ModifiedDate_ID,
                    SMGXJPlanTicketModel.RequestID_ID,
                    SMGXJPlanTicketModel.ShortDescription_ID,
                    SMGXJPlanTicketModel.Submitter_ID,
                    SMGXJPlanTicketModel.cChr_TicketID_ID
                }).FirstOrDefault();
            return list;
        }

        public string Create(SMGXJPlanTicketModel model)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanTicketModel>();
                proxy.CreateEntry(model);
                return "Success";
            }
            catch (Exception e)
            {
                throw e;
                //return "Fail :" + e.Message;
            }
           
        }

        public string Modify(string id, SMGXJPlanTicketModel model)
        {
            try
            {
                var proxy = this.GetARProxy<SMGXJPlanTicketModel>();
                proxy.SetEntryByQuery(model,"\'Request ID\' = \""+id+"\"",BMC.ARSystem.Server.SetEntryByQuery_NoMatchOption.NoAction,BMC.ARSystem.Server.SetEntryByQuery_MultiMatchOption.ModifyFirst);
            }
            catch (Exception e)
            {
                return "Fail :" + e.Message;
            }
            return "Success";
        }

        public IEnumerable<SMGXJPlanTicketModel> GetList(string Qual)
        {
            var proxy = this.GetARProxy<SMGXJPlanTicketModel>();
            var list = proxy.GetListEntry(Qual, new System.Collections.Generic.List<uint>()
                {
                    SMGXJPlanTicketModel.RequestID_ID,
                    SMGXJPlanTicketModel.cChr_Department_ID,
                    SMGXJPlanTicketModel.cChr_Description_ID,
                    SMGXJPlanTicketModel.cChr_FullName_ID,
                    SMGXJPlanTicketModel.cChr_MarkAsDelete_ID,
                    SMGXJPlanTicketModel.cChr_Name_ID,
                    SMGXJPlanTicketModel.cChr_Office_ID,
                    SMGXJPlanTicketModel.CreateDate_ID,
                    SMGXJPlanTicketModel.LastModifiedBy_ID,
                    SMGXJPlanTicketModel.ModifiedDate_ID,
                    SMGXJPlanTicketModel.RequestID_ID,
                    SMGXJPlanTicketModel.ShortDescription_ID,
                    SMGXJPlanTicketModel.Submitter_ID,
                    SMGXJPlanTicketModel.cChr_TicketID_ID,
                    SMGXJPlanTicketModel.Status_ID
                });
            return list;
        }
    }
}
